<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>侦听器</title>
</head>
<body>
  <div id="app">
    <input type="text" v-model.lazy="username">
    <h2>{{msg}}</h2>
  </div>
  <script src="vue.js"></script>
  <script>
    const vm = new Vue({
      el:"#app",
      data:{
        username:'',
        users:["admin","lisi","wangwu","zl"],
        msg:''
      },
      watch:{
        username(newVal,oldVal) {
          let index = this.users.findIndex(e=>e==newVal)
          if(index>=0) this.msg='该用户已经存在，不能注册!' 
          else this.msg = "该用户可以注册"
        }
      }
    })
  </script>
</body>
</html>